﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Data;
using System.Windows.Controls.Primitives;

namespace CooStationsToolsNew
{
    /// <summary>
    /// NewProject.xaml 的交互逻辑
    /// </summary>
    public partial class NewProject : Window
    {
        ProjectTools pt = new ProjectTools();
        Popup pop = new Popup();
        TextBlock tb = new TextBlock();
        public static bool isflag=false;
        public NewProject()
        {
            InitializeComponent();
            //TextBlock tb = new TextBlock();

            ImageBrush ib = new ImageBrush();
            ib.ImageSource = new BitmapImage(new Uri("pic/popo.png", UriKind.RelativeOrAbsolute));
            ib.Stretch = Stretch.Fill;
            tb.Background = ib;
            tb.Width = 157.0;
            tb.Height = 62.0;
            tb.TextAlignment = TextAlignment.Center;
            tb.Padding = new Thickness(5.0);
           
            pop.Width = 157.0;
            pop.Height = 62.0;
            pop.Child = tb;
            pop.AllowsTransparency = true;
            pop.StaysOpen = false;
           
            pop.Placement = PlacementMode.Top;
            pop.PopupAnimation = PopupAnimation.Fade;
        }

        private void NewProCancelBt_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void NewProBt_Click(object sender, RoutedEventArgs e)
        {
            if (txtName.Text != "")
            {
                App.proName = txtName.Text;
                if (txtPath.Text != "")
                {
                    DirectoryInfo dr = new DirectoryInfo(txtPath.Text + "\\" + txtName.Text);
                    if (!dr.Exists)
                    {
                        try
                        {
                            dr.Create();
                        }
                        catch
                        {
                            System.Windows.MessageBox.Show("建立文件夹失败,请检查地址填写是否正确");
                            return;
                        }
                        App.proPath = dr.FullName;
                    }
                    else {
                        App.proPath = dr.FullName;
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("请填写地址");
                    return;
                }
            }
            else
            {
                System.Windows.MessageBox.Show("请填写名称");
                return;
            }

            string width = txtWid.Text;
            string height = txtHei.Text;
            if (width != "" && height != "")
            {
                CreatProject(width, height);
                return;
            }
            else
            {
                int indexRe = ComResolution.SelectedIndex;

                if (indexRe > 0)
                {
                    string Resolution = ComResolution.SelectionBoxItem.ToString();
                    int n = Resolution.IndexOf('*');

                    string rewidth = Resolution.Substring(0, n);
                    string reheight = Resolution.Substring(n + 1);

                    CreatProject(rewidth, reheight);
                }
                else
                {
                    System.Windows.MessageBox.Show("请填写完整的宽高或者选择分辨率!");
                    return;
                }

            }

 
        }

        private void OpenPathBt_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            fb.ShowNewFolderButton = true;
            fb.RootFolder = System.Environment.SpecialFolder.MyComputer;
         
            fb.Description = "请选择目录";
            fb.ShowDialog();
            if (fb.SelectedPath != "")
            {
                txtPath.Text = fb.SelectedPath;
            }

        }

        #region  创建项目相关
        /// <summary>
        /// 创建项目
        /// </summary>
        /// <param name="path"></param>
        private void CreatProject( string width,string height)
        {
            try
            {
                App.dt.Clear();
                App.eventdt.Clear();
                App.piclist.Clear();
                App.rectlist.Clear();
                App.Pagelist.Clear();
                App.ActionList.Clear();
                App.divlist.Clear();
                App.scriptdt.Clear();
            }
            catch
            {
            }
            pt.CreatCfyXml(App.proPath + "\\cfy.xml", App.proPath + "\\cfy_event.xml");
            pt.CreatCfyXml(App.appPath + "\\temp\\cfy.xml", App.appPath + "\\temp\\cfy_event.xml");

            App.proWidth = width;
            App.proHeight = height;

            XmlDocument Mydoc = new XmlDocument();
            XmlDeclaration Mydec = Mydoc.CreateXmlDeclaration("1.0", "utf-8", null);
            Mydoc.AppendChild(Mydec);
            XmlElement root = Mydoc.CreateElement("root");
            Mydoc.AppendChild(root);

            XmlElement xePath = Mydoc.CreateElement("ProPath");
            xePath.InnerXml = App.proPath;
            root.AppendChild(xePath);

            XmlElement xeName = Mydoc.CreateElement("ProName");
            xeName.InnerXml = App.proName;
            root.AppendChild(xeName);

            XmlElement xeType = Mydoc.CreateElement("type");
            xeType.InnerXml = App.proType;
            root.AppendChild(xeType);

            XmlElement xewidth = Mydoc.CreateElement("width");
            xewidth.InnerXml = width;
            root.AppendChild(xewidth);

            XmlElement xeheight = Mydoc.CreateElement("height");
            xeheight.InnerXml = height;
            root.AppendChild(xeheight);


            XmlElement xefile = Mydoc.CreateElement("file");

            root.AppendChild(xefile);

       //     addFileList();
            XmlElement xeFape = Mydoc.CreateElement("page");
            root.AppendChild(xeFape);

            XmlElement xe = Mydoc.CreateElement("name");
            xe.InnerXml = "cfy";
            xeFape.AppendChild(xe);

            Model.CfyPage cp = new Model.CfyPage();
            cp.PageName = "cfy";
            cp.Id = "page" + pt.GetRandom();
            cp.Width = App.proWidth;
            cp.Height = App.proHeight;
            App.Pagelist.Add(cp);
            Mydoc.Save(App.proPath + "\\" + App.proName + ".cfy");

            if (!Directory.Exists(App.proPath + "//pic"))
            {
                Directory.CreateDirectory(App.proPath + "//pic");
            }


            CopyFile();

         
            pt.AddPastUse();

        

            pt.CopyFile(App.proPath, App.appPath + "\\temp");
            isflag = true;
            DataTable dt = new DataTable();
            dt.Columns.Add("type");
            dt.Columns.Add(new DataColumn("object", typeof(Object)));
            App.dt = dt;

            DataTable eventdt = new DataTable();
            eventdt.Columns.Add("ItemID");
            eventdt.Columns.Add(new DataColumn("object", typeof(Object)));
            App.scriptdt = App.eventdt = eventdt;

            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// 文件列表
        /// </summary>
        private void addFileList()
        {
            DirectoryInfo dir = new DirectoryInfo(App.proPath);

            FileInfo[] filelist =  dir.GetFiles();
            XmlDocument Mydoc = new XmlDocument();
            Mydoc.Load(App.proPath + "\\" + App.proName + ".cfy");
            XmlElement root = (XmlElement)Mydoc.ChildNodes[1];
            XmlElement xeFileList = Mydoc.CreateElement("FileList");
            root.AppendChild(xeFileList);

            for (int i = 0; i < filelist.Length; i++)
            {
                FileInfo f = filelist[i];
                XmlElement xe = Mydoc.CreateElement("file");
                xe.InnerXml = f.FullName;
                xeFileList.AppendChild(xe);
            }

            Mydoc.Save(App.proPath + "\\" + App.proName + ".cfy");

        }

        /// <summary>
        /// 复制必要文件到项目
        /// </summary>
        private void CopyFile()
        {
            File.Copy(App.appPath + "\\temp\\cfyconfig.xml", App.proPath + "\\cfyconfig.xml", true);
            File.Copy(App.appPath + "\\temp\\CoolMainWeb.swf", App.proPath + "\\CoolMainWeb.swf", true);
            File.Copy(App.appPath + "\\temp\\index.htm", App.proPath + "\\index.htm", true);
            File.Copy(App.appPath + "\\temp\\MIME.ini", App.proPath + "\\MIME.ini", true);
        }


        #endregion

        private void lbtn_close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void txtName_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // System.Windows.MessageBox.Show(e.Key.ToString());
            System.Windows.Controls.TextBox txt = sender as System.Windows.Controls.TextBox;
            //屏蔽非法按键
            if((e.Key<Key.A || e.Key>Key.Z) && txt.Text=="" )
            {
                e.Handled = true;
                pop.PlacementTarget = txtName;
                tb.Text = "不能以数字开头!";
                pop.IsOpen = true;
            }
            else if (e.Key == Key.Oem5 || e.Key == Key.OemQuestion || e.Key==Key.Divide || e.Key==Key.Multiply)
            {
                e.Handled = true;
                pop.PlacementTarget = txtName;
                tb.Text = "不能以数字开头!";
                pop.IsOpen = true;
            }
            else if ((e.Key == Key.D8  || e.Key == Key.OemPeriod || 
                    e.Key == Key.Oem5 || e.Key == Key.Oem1 || e.Key == Key.Oem7 || 
                    e.Key == Key.OemQuestion || e.Key==Key.OemComma) && e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                e.Handled = true;
                pop.PlacementTarget = txtName;
                tb.Text = "不能以数字开头!";
                pop.IsOpen = true;
            }
        }

        private void txtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            //屏蔽中文输入和非法字符粘贴输入
            //System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
            //TextChange[] change = new TextChange[e.Changes.Count];
            //e.Changes.CopyTo(change, 0);

            //int offset = change[0].Offset;
            //if (change[0].AddedLength > 0)
            //{
            //    double num = 0;
            //    bool isflag=true;
            //    for (int i = 0; i < textBox.Text.Length; i++)
            //    {
            //        if ((textBox.Text)[i] == '?' || (textBox.Text)[i] == '/' 
            //            ||(textBox.Text)[i] == '|' ||(textBox.Text)[i] == '<' 
            //            || (textBox.Text)[i] == '>'||(textBox.Text)[i] == '*' 
            //            ||(textBox.Text)[i] == ':' ||(textBox.Text)[i] == '"' 
            //            ||(textBox.Text)[i] == '\\')
            //        {
            //            textBox.Text.Remove(i, 1);
            //        }
            //    }
            //        //if (!Double.TryParse(textBox.Text, out num))
            //        //{
            //        //    textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
            //        //    textBox.Select(offset, 0);
            //        //}
            //}
        }

        private void txtHei_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if ((e.Key < Key.D0 || e.Key > Key.D9) && (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) && e.Key != Key.Back && e.Key != Key.Tab)
            {
                e.Handled = true;
                pop.PlacementTarget = txtHei;
                tb.Text = "只能输入数字!";
                pop.IsOpen = true;
            } 
        }

        private void txtWid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if ((e.Key < Key.D0 || e.Key > Key.D9) && (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) && e.Key != Key.Back && e.Key != Key.Tab)
            {
                e.Handled = true;
                pop.PlacementTarget = txtWid;
                tb.Text = "只能输入数字!";
                pop.IsOpen = true;
            }
        }
    }
}
